﻿@inherits AdminCompontentBase

<MNavigationDrawer Stateless Touchless Temporary Fixed Right Value=Visible ValueChanged=VisibleChanged Width="465" Class="pa-6">
    <div class="block-between mb-12">
        <span class="text-h6">@T("Add Role")</span>
        <MIcon Color="neutral-lighten-3" Size=24 OnClick="async ()=> await UpdateVisible(false)">mdi-close</MIcon>
    </div>

    <MForm Model=Page.CurrentData EnableValidation="true">
        <MTextField @bind-Value="Page.CurrentData.Name" Class="mb-6" Label="@T("Role.Name")" HideDetails="@("auto")" Outlined />
        <MTextField @bind-Value="Page.CurrentData.Number" Class="mb-6" Label="@T("Role.Number")" HideDetails="@("auto")" Outlined />
        <MTextarea @bind-Value="Page.CurrentData.Description" Class="mb-6" Label="@T("Description")" HideDetails="@("auto")" Outlined />
        <div style="bottom:48px;right:24px;position:absolute">
            <MButton MinWidth=80 Height=40 Outlined Class="text-btn rounded-pill" OnClick="async() => await UpdateVisible(false)">@T("Cancel")</MButton>
            <MButton MinWidth=80 Height=40 Color="primary" Class="ml-6 rounded-pill" OnClick="async ()=> await AddOrUpdateAsync(context)">@T("Confirm")</MButton>
        </div>
    </MForm>
</MNavigationDrawer>

@code {
    [Inject]
    public RolePage Page { get; set; } = default!;

    [Parameter]
    public bool? Visible { get; set; }

    [Parameter]
    public EventCallback<bool?> VisibleChanged { get; set; }

    private async Task UpdateVisible(bool visible)
    {
        if (VisibleChanged.HasDelegate)
        {
            await VisibleChanged.InvokeAsync(visible);
        }
        else
        {
            Visible = visible;
        }
    }

    private async Task AddOrUpdateAsync(FormContext context)
    {
        if (context.Validate())
        {
            var success = await Page.AddAsync();
            if(success) await UpdateVisible(false);
        }
    }
}
